<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- 
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements.  See the NOTICE file
distributed with this work for additional information
regarding copyright ownership.  The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License.  You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied.  See the License for the
specific language governing permissions and limitations
under the License. 
-->
<html>
<head>
    <link type="text/css" rel="stylesheet" href="https://struts.apache.org/css/default.css">
    <style type="text/css">
        .dp-highlighter {
            width:95% !important;
        }
    </style>
    <style type="text/css">
        .footer {
            background-image:      url('https://cwiki.apache.org/confluence/images/border/border_bottom.gif');
            background-repeat:     repeat-x;
            background-position:   left top;
            padding-top:           4px;
            color:                 #666;
        }
    </style>
    <link href='https://struts.apache.org/highlighter/style/shCoreStruts.css' rel='stylesheet' type='text/css' />
    <link href='https://struts.apache.org/highlighter/style/shThemeStruts.css' rel='stylesheet' type='text/css' />
    <script src='https://struts.apache.org/highlighter/js/shCore.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushPlain.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushXml.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJava.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushJScript.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushGroovy.js' type='text/javascript'></script>
    <script src='https://struts.apache.org/highlighter/js/shBrushBash.js' type='text/javascript'></script>
    <script type="text/javascript">
        SyntaxHighlighter.defaults['toolbar'] = false;
        SyntaxHighlighter.all();
    </script>
    <script type="text/javascript" language="javascript">
        var hide = null;
        var show = null;
        var children = null;

        function init() {
            /* Search form initialization */
            var form = document.forms['search'];
            if (form != null) {
                form.elements['domains'].value = location.hostname;
                form.elements['sitesearch'].value = location.hostname;
            }

            /* Children initialization */
            hide = document.getElementById('hide');
            show = document.getElementById('show');
            children = document.all != null ?
                    document.all['children'] :
                    document.getElementById('children');
            if (children != null) {
                children.style.display = 'none';
                show.style.display = 'inline';
                hide.style.display = 'none';
            }
        }

        function showChildren() {
            children.style.display = 'block';
            show.style.display = 'none';
            hide.style.display = 'inline';
        }

        function hideChildren() {
            children.style.display = 'none';
            show.style.display = 'inline';
            hide.style.display = 'none';
        }
    </script>
    <title>Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application</title>
</head>
<body onload="init()">
<table border="0" cellpadding="2" cellspacing="0" width="100%">
    <tr class="topBar">
        <td align="left" valign="middle" class="topBarDiv" align="left" nowrap>
            &nbsp;<a href="home.html">Home</a>&nbsp;&gt;&nbsp;<a href="tutorials.html">Tutorials</a>&nbsp;&gt;&nbsp;<a href="getting-started.html">Getting Started</a>&nbsp;&gt;&nbsp;<a href="how-to-create-a-struts-2-web-application.html">How To Create A Struts 2 Web Application</a>&nbsp;&gt;&nbsp;<a href="create-struts-2-web-application-using-maven-to-manage-artifacts-and-to-build-the-application.html">Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application</a>
        </td>
        <td align="right" valign="middle" nowrap>
            <form name="search" action="https://www.google.com/search" method="get">
                <input type="hidden" name="ie" value="UTF-8" />
                <input type="hidden" name="oe" value="UTF-8" />
                <input type="hidden" name="domains" value="" />
                <input type="hidden" name="sitesearch" value="" />
                <input type="text" name="q" maxlength="255" value="" />
                <input type="submit" name="btnG" value="Google Search" />
            </form>
        </td>
    </tr>
</table>

<div id="PageContent">
    <div class="pageheader" style="padding: 6px 0px 0px 0px;">
        <!-- We'll enable this once we figure out how to access (and save) the logo resource -->
        <!--img src="/wiki/images/confluence_logo.gif" style="float: left; margin: 4px 4px 4px 10px;" border="0"-->
        <div style="margin: 0px 10px 0px 10px" class="smalltext">Apache Struts 2 Documentation</div>
        <div style="margin: 0px 10px 8px 10px"  class="pagetitle">Create Struts 2 Web Application Using Maven To Manage Artifacts and To Build The Application</div>

        <div class="greynavbar" align="right" style="padding: 2px 10px; margin: 0px;">
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=14811868">
                <img src="https://cwiki.apache.org/confluence/images/icons/notep_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Edit Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/editpage.action?pageId=14811868">Edit Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">
                <img src="https://cwiki.apache.org/confluence/images/icons/browse_space.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Browse Space"></a>
            <a href="https://cwiki.apache.org/confluence/pages/listpages.action?key=WW">Browse Space</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=14811868">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_page_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add Page"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createpage.action?spaceKey=WW&fromPageId=14811868">Add Page</a>
            &nbsp;
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=14811868">
                <img src="https://cwiki.apache.org/confluence/images/icons/add_blogentry_16.gif"
                     height="16" width="16" border="0" align="absmiddle" title="Add News"></a>
            <a href="https://cwiki.apache.org/confluence/pages/createblogpost.action?spaceKey=WW&fromPageId=14811868">Add News</a>
        </div>
    </div>

    <div class="pagecontent">
        <div class="wiki-content">
            <div id="ConfluenceContent"><p>You can checkout this complete example project (and all the other Getting Started tutorial example projects) from the Struts 2 GitHub repository at <a shape="rect" class="external-link" href="https://github.com/apache/struts-examples" rel="nofollow">https://github.com/apache/struts-examples</a>. The example projects use Maven&#160;to manage the artifact dependencies and to build the .war files.</p><div class="confluence-information-macro confluence-information-macro-information"><span class="aui-icon aui-icon-small aui-iconfont-info confluence-information-macro-icon"></span><div class="confluence-information-macro-body"><p>This tutorial assumes you know how to create a Java web application that uses Maven to manage artifacts and build the web application archive (war) file.</p></div></div><h3 id="CreateStruts2WebApplicationUsingMavenToManageArtifactsandToBuildTheApplication-Step1-CreateAJavaWebApplication">Step 1 - Create A Java Web Application</h3><p>In your Java IDE create a Java web application with a project name of basic_struts that follows the standard Maven project folder structure. In your pom.xml include the following:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>pom.xml build node</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">	&lt;build&gt;
		&lt;finalName&gt;basic_struts&lt;/finalName&gt;
	&lt;/build&gt;
</pre>
</div></div><h3 id="CreateStruts2WebApplicationUsingMavenToManageArtifactsandToBuildTheApplication-Step2-Addindex.jsp">Step 2 - Add index.jsp</h3><p>Our next step is to add a simple index.jsp to this web application. Create an index.jsp under src/main/webapp with a title of "Basic Struts 2 Application - Welcome" and in the body add an h1 heading of "Welcome to Struts 2!"</p><p>Run mvn clean package to create the war file. Copy the war file into your Servlet container so that it will deploy the war file.</p><p>Start up your Servlet container and in a web browser go to <a shape="rect" class="external-link" href="http://localhost:8080/Basic_Struts2_Mvn/index.jsp" rel="nofollow">http://localhost:8080/Basic_Struts2_Mvn/index.jsp</a>. You should see the following:</p><p><span class="confluence-embedded-file-wrapper"><img class="confluence-embedded-image" src="create-struts-2-web-application-using-maven-to-manage-artifacts-and-to-build-the-application.data/Basic_Struts2_Mvn.png"></span></p><h3 id="CreateStruts2WebApplicationUsingMavenToManageArtifactsandToBuildTheApplication-Step3-AddStruts2JarFilesToClassPath">Step 3 - Add Struts 2 Jar Files To Class Path</h3><p>Now that we know we have a working Java web application, lets add the minimal required Struts 2 framework Jar files to our web application's class path. In pom.xml add the following dependency node:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>pom.xml dependency node</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;dependency&gt;
	&lt;groupId&gt;org.apache.struts&lt;/groupId&gt;
	&lt;artifactId&gt;struts2-core&lt;/artifactId&gt;
	&lt;version&gt;X.X.X.X&lt;/version&gt;
&lt;/dependency&gt;

</pre>
</div></div><p>Of course replace the X.X.X.X with the current Struts 2 version. Maven will get the struts2-core jar and the other jar files struts2-core requires (transitive dependencies). (NOTE: Beginning with Struts version 2.2.3 you do not need to specify a separate dependency node for javassist.)</p><h3 id="CreateStruts2WebApplicationUsingMavenToManageArtifactsandToBuildTheApplication-Step4-AddLogging">Step 4 - Add Logging</h3><p>To see what's happening under the hood, the example application for this tutorial uses log4j. You'll need to add to pom.xml a dependency node for the log4j jar file:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>pom.xml log4j dependency node</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;dependency&gt;
	&lt;groupId&gt;log4j&lt;/groupId&gt;
	&lt;artifactId&gt;log4j&lt;/artifactId&gt;
	&lt;version&gt;1.2.14&lt;/version&gt;
&lt;/dependency&gt;
</pre>
</div></div><p>Setup a log4j.xml configuration in the src/main/resources folder. You can copy the one from the example application, which contains the following</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>log4j.xml</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;?xml version="1.0" encoding="UTF-8" ?&gt;
&lt;!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd"&gt;

&lt;log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"&gt;
    
    &lt;appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"&gt;
       &lt;layout class="org.apache.log4j.PatternLayout"&gt; 
          &lt;param name="ConversionPattern" value="%d %-5p %c.%M:%L - %m%n"/&gt; 
       &lt;/layout&gt; 
    &lt;/appender&gt;
 
    &lt;!-- specify the logging level for loggers from other libraries --&gt;
    &lt;logger name="com.opensymphony"&gt;
    	&lt;level value="DEBUG" /&gt;
    &lt;/logger&gt;

    &lt;logger name="org.apache.struts2"&gt;
    	 &lt;level value="DEBUG" /&gt;
    &lt;/logger&gt;
  
   &lt;!-- for all other loggers log only info and above log messages --&gt;
     &lt;root&gt;
        &lt;priority value="INFO"/&gt; 
        &lt;appender-ref ref="STDOUT" /&gt; 
     &lt;/root&gt; 
    
&lt;/log4j:configuration&gt; 
</pre>
</div></div><p>Note the above log4j configuration specifies the console as the log target.</p><h3 id="CreateStruts2WebApplicationUsingMavenToManageArtifactsandToBuildTheApplication-Step5-AddStruts2ServletFilter">Step 5 - Add Struts 2 Servlet Filter</h3><p>To enable the Struts 2 framework to work with your web application you need to add a Servlet filter class and filter mapping to web.xml. Below is the filter and filter-mapping nodes you should add to web.xml.</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>web.xml Servlet Filter</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;filter&gt;
  &lt;filter-name&gt;struts2&lt;/filter-name&gt;
  &lt;filter-class&gt;org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter&lt;/filter-class&gt;
&lt;/filter&gt;

&lt;filter-mapping&gt;
  &lt;filter-name&gt;struts2&lt;/filter-name&gt;
   &lt;url-pattern&gt;/*&lt;/url-pattern&gt;
&lt;/filter-mapping&gt;
</pre>
</div></div><p>For more information about configuring the deployment descriptor for Struts 2 see <a shape="rect" href="webxml.html">web.xml</a>. Note the url-pattern node value is /* meaning the Struts 2 filter will be applied to all URLs for this web application.</p><h3 id="CreateStruts2WebApplicationUsingMavenToManageArtifactsandToBuildTheApplication-Step6-Createstruts.xml">Step 6 - Create struts.xml</h3><p>Struts 2 can use either an XML configuration file or annotations (or both) to specify the relationship between a URL, a Java class, and a view page (such as index.jsp). For our basic Struts 2 application, we'll use a minimal xml configuration. Note the file name is struts.xml and it should be in the src/main/resources folder (struts.xml must be on the web application's root class path).</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>struts.xml</b></div><div class="codeContent panelContent pdl">
<pre class="brush: xml; gutter: false; theme: Default" style="font-size:12px;">&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd"&gt;

&lt;struts&gt;

	&lt;constant name="struts.devMode" value="true" /&gt;

	&lt;package name="basicstruts2" extends="struts-default"&gt;

		&lt;action name="index"&gt;
			&lt;result&gt;/index.jsp&lt;/result&gt;
		&lt;/action&gt;

	&lt;/package&gt;

&lt;/struts&gt;
</pre>
</div></div><p>This minimal Struts 2 configuration file tells the framework that if the URL ends in index.action to redirect the browser to index.jsp.</p><p>For more information about the struts.xml configuration file see <a shape="rect" href="strutsxml.html">struts.xml</a>.</p><h3 id="CreateStruts2WebApplicationUsingMavenToManageArtifactsandToBuildTheApplication-Step7-BuildandRuntheApplication">Step 7 - Build and Run the Application</h3><p>With all of the above in place run mvn clean package to create the war file. Remove the previously created war file and exploded web application folder from your Servlet container's webapps folder. Copy to your Servlet container's webapps folder the new war you just created.</p><p>Start up the Servlet container. View the console where you should see numerous debug messages that tell you the Struts 2 framework is being included in the Basic_Struts2_Mvn web application.</p><p>Open a web browser and go to <a shape="rect" class="external-link" href="http://localhost:8080/Basic_Struts2_Mvn/index.action" rel="nofollow">http://localhost:8080/Basic_Struts2_Mvn/index.action</a> (note that's index.action not index.jsp at the end of the URL). You should see the same web page as when going to <a shape="rect" class="external-link" href="http://localhost:8080/Basic_Struts2_Mvn/index.jsp" rel="nofollow">http://localhost:8080/Basic_Struts2_Mvn/index.jsp</a>. View the log messages written to the console and you should find several that discuss index.action and index.jsp:</p><div class="code panel pdl" style="border-width: 1px;"><div class="codeHeader panelHeader pdl" style="border-bottom-width: 1px;"><b>Struts 2 Log Messages</b></div><div class="codeContent panelContent pdl">
<pre class="brush: java; gutter: false; theme: Default" style="font-size:12px;">com.opensymphony.xwork2.DefaultActionProxy.debug:57 - Creating an DefaultActionProxy for namespace / and action name index
...
org.apache.struts2.dispatcher.ServletDispatcherResult.debug:57 - Forwarding to location /index.jsp
</pre>
</div></div><p>Note that the xwork2 artifact is one of the Jar files that is a transitive dependency for the struts2-core Jar file. The xwork2 library is used heavily by the Struts 2 framework.</p><h3 id="CreateStruts2WebApplicationUsingMavenToManageArtifactsandToBuildTheApplication-GettingHelp">Getting Help</h3><p>The <a shape="rect" class="external-link" href="http://struts.apache.org/mail.html">Struts 2 user mailing list</a> is an excellent place to get help. If you are having a problem getting this Basic Struts 2 application to work search the Struts 2 mailing list. If you don't find an answer to your problem, post a question on the mailing list.</p><div class="table-wrap"><table class="confluenceTable"><tbody><tr><th colspan="1" rowspan="1" class="confluenceTh"><p>Next</p></th><td colspan="1" rowspan="1" class="confluenceTd"><p>Onward to <a shape="rect" href="hello-world-using-struts-2.html">Hello World Using Struts 2</a></p></td></tr><tr><th colspan="1" rowspan="1" class="confluenceTh"><p>Prev</p></th><td colspan="1" rowspan="1" class="confluenceTd"><p>Return to <a shape="rect" href="tutorials.html">Tutorials</a></p></td></tr></tbody></table></div></div>
        </div>

        
    </div>
</div>
<div class="footer">
    Generated by CXF SiteExporter
</div>
</body>
</html>
